const LIGA = './lig.json'
const CALT = './SansCode.calt'
const fs = require('fs')
const _ = require('lodash')
const quote = str => str + "'"

const genIgnore = chars => {
    let q = [quote(chars[0]), ...chars.slice(1)]
    let q1 = [chars[0], ...q]
    let q2 = [...q, _.last(chars)]
    return [q1, q2]
}
const genSPC = chars => {
    let ret = []
    for (let i = 0; i < chars.length - 1; i++) {
        let pre = chars.slice(0, i).map(x => 'LIG')
        let mid = quote(chars[i])
        let post = chars.slice(i + 1, chars.length)
        let s = [...pre, mid, ...post]
        ret.unshift(`sub ${s.join(' ')} by LIG ;`)
    }
    ret.unshift(`sub ${chars.slice(0,-1).map(x => 'LIG').join(' ')} ${quote(_(chars).last())} by ${chars.join('_')}.liga ;`)
    return ret
}
const genSPC12 = chars => {
    let ret = []
    for (let i = 0; i < chars.length - 1; i++) {
        let pre = chars.slice(0, i).map(x => 'LIG2')
        let mid = quote(chars[i])
        let post = chars.slice(i + 1, chars.length)
        let s = [...pre, mid, ...post]
        ret.unshift(`sub ${s.join(' ')} by LIG2 ;`)
    }
    ret.unshift(`sub ${chars.slice(0,-1).map(x => 'LIG2').join(' ')} ${quote(_(chars).last())} by ${chars.join('_')}.liga ;`)
    return ret
}
const genSPC01 = chars => {
    return [`sub ${chars.join(' ')} by ${chars.join('_')}.liga ;`]
}

const slide = (src, tg, comp) => {
    let ret = []
    let c = comp || ((x, y) => x === y)
    for (let i = 0; i < src.length; i++) {
        if (c(src[i], tg[0])) {
            let r = true
            for (let j = 1; j < tg.length; j++) {
                r = r && c(src[i + j], tg[j])
            }
            ret.push(i)
        }
    }
    return ret.length > 0 ? ret : null
}
const genSup = (chars, sup) => {
    if (!sup) return []
    let ret = sup.map(x => {
        let t = x.split('_')
        let a = slide(t, chars)
        let head, tail
        if (a === 0) {
            head = []
        } else {
            head = t.slice(0, a)
        }
        tail = t.slice(a, t.length)
        let j = [...head, quote(tail[0]), ...tail.slice(1)]
        return j
    })
    return ret
}
const spcTable = {
    '1/1': genSPC,
    '0/1': genSPC01,
    '1/2': genSPC12,
}
const genCALT = (chars, sup) => {
    let uniqIgnore = _.uniqWith([...genIgnore(chars[0]), ...genSup(chars[0], sup)], (x, y) => _.isEqual(x, y))
    let ignore = chars[1] === '0/1' ? [] : uniqIgnore.map(x => `ignore sub ${x.join(' ')} ;`)
    let SPC = (spcTable[chars[1]] || genSPC)(chars[0])
    return [chars[0].join('_'), ...ignore, ...SPC]
}


let charss = require(LIGA)

let charTable = charss.map(x => {
    return x[0].join('_')
})

let sortedCharss = charss.sort((x, y) => y[0].length - x[0].length)

let result = sortedCharss.map(x => {
    let k = x[0].join('_')
    let f = charTable.filter(y => y.length !== k.length && y.match(new RegExp(k)))
    let r = f.length > 0 && f
    let ret = genCALT(x, r)
    return ret
})




fs.writeFileSync(CALT, result.map(x => `lookup ${x[0]} {\n${x.slice(1).map(x => `  ${x}`).join('\n')}\n} ${x[0]};`).join('\n'))